import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import { fileURLToPath } from "url";
import Components from "unplugin-vue-components/vite";
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		Components({
			dirs: ["src/components"],
			resolvers: [ElementPlusResolver()],
		}),
		AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
	],
	base: "./",
	resolve: {
		alias: [
			{
				find: "@",
				replacement: resolve(__dirname, "src")
			}
		]
	},

	// tauri
	server: {
		host: true,
		port: 8081,
		// open: true,
		// Tauri 使用固定端口，若此端口不可用将会导致程序错误
		strictPort: true
	},
	// 防止 vite 输出复杂的 rust 错误
	clearScreen: false,
	// 使用 `TAURI_PLATFORM`、`TAURI_ARCH`、`TAURI_FAMILY`,
	// `TAURI_PLATFORM_VERSION`、`TAURI_PLATFORM_TYPE` 和 `TAURI_DEBUG` 环境变量
	envPrefix: ["VITE_", "TAURI_"],
	build: {
		// Tauri 支持 es2021
		target: ["es2021", "chrome100", "safari13"],
		// 不为调试构建压缩构建体积
		minify: !process.env.TAURI_DEBUG ? "esbuild" : false,
		// 为调试构建生成源代码映射 (sourcemap)
		sourcemap: !!process.env.TAURI_DEBUG
	}
});
